package service;

import com.alibaba.fastjson.JSON;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import utils.MyFileUtils;
import utils.OrcUtils;
import utils.WaitUtils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;

@DisplayName("腾讯新开城市")
public class GetTXNewCity {
    @Test
    public void get() throws MalformedURLException, InterruptedException {

        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("platformVersion", "7");
        desiredCapabilities.setCapability("deviceName", "xx");
//        desiredCapabilities.setCapability("appPackage", "com.ygkj.chelaile.standard");
//        desiredCapabilities.setCapability("appActivity", "dev.xesam.chelaile.app.module.func.SplashActivity");
        desiredCapabilities.setCapability("noReset", true);
        desiredCapabilities.setCapability("desiredCapabilities", true);//可以输入中文
        //  desiredCapabilities.setCapability("app","D:\\my-mayun\\apk\\chelaile_app_chelaile_www_lastest.apk");
        //  desiredCapabilities.setCapability("autoGrantPermissions",true);//自动同意一些授权
        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        AndroidDriver driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        Activity a = new Activity("com.tencent.map", "WelcomeActivity");
        driver.startActivity(a);
        WaitUtils.sleep(10);
        MobileElement el1 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/androidx.drawerlayout.widget.DrawerLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[4]");
        el1.click();
        WaitUtils.sleep(5);
        MobileElement el2 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.View");
        el2.click();
        WaitUtils.sleep(5);
        takeScreenShot(driver);
        for (int i = 0; i < 10; i++) {
            (new TouchAction(driver)).press(PointOption.point(200, 1632)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
                    .moveTo(PointOption.point(200, 601)).release().perform();
            WaitUtils.sleep(2);
            takeScreenShot(driver);
        }

        /**
         * orc识别
         */
        Collection<File> list = MyFileUtils.getFile("D:\\AutoScreenCapture");
        List<String> cityList = new ArrayList<>();
        for (File f : list) {
            System.out.println(f.getAbsolutePath());
            List<String> tmpCity = OrcUtils.readList(f.getAbsolutePath());
            for (String tmp : tmpCity) {
                if (!cityList.contains(tmp)) {
                    cityList.add(tmp);
                }
            }
        }
        System.out.println(JSON.toJSONString(cityList));
        WaitUtils.sleep(10);
    }

    public static void takeScreenShot(AndroidDriver<WebElement> driver) {
        File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(screenShotFile, new File("D:\\AutoScreenCapture\\" + getCurrentDateTime("txdt_") + ".PNG"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getCurrentDateTime(String name) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");//设置日期格式
        return name + df.format(new Date());
    }


}
